package com.kedi.productcenter.service.impl;

import com.kedi.productcenter.mapper.BlsMapper;
import com.kedi.productcenter.service.BlsProductService;
import com.kedi.productcenter.write.mapper.WriteBlsMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
/**
* @Description:    比南山商品 实现类
* @Author:         ytw
* @CreateDate:     2019/7/22 16:02
* @UpdateRemark:   修改内容
* @Version:        1.0
*/
@Service
public class BlsProductServiceImpl implements  BlsProductService{

    private static final List<String> CONTENT_TYPE= Arrays.asList("image/gif","image/jpeg","image/png");
    private static final Logger LOGGER=LoggerFactory.getLogger(BlsProductServiceImpl.class);
    @Autowired
    BlsMapper mapper;
    @Autowired
    WriteBlsMapper writeMapper;

    /**
    * 方法实现说明  查找所有商品
    * @author      ytw
    *@param
    * @return
    * @exception   
    * @date        2019/7/22 16:03
    */
    @Override
    public List<Map<String, Object>> findList() {
        return mapper.findList();
    }

    /**
    * 方法实现说明  根据
    * @author      ytw
    *@param        num
    * @return
    * @exception   
    * @date        2019/7/22 16:04
    */
    @Override
    public List<Map<String, Object>> findbyStatus(Integer num) {
        return mapper.findbyStatus(num);
    }

    /**
    * 方法实现说明
    * @author      ytw
    *@param         opusid,puserid
    * @return
    * @exception   
    * @date        2019/7/22 16:15
    */
    @Override
    public Map<String, Object> findDetail(Integer opusid,Integer puserid) {
        Integer  userPraise = mapper.findUserPraise(opusid,puserid);
        Integer userColection = mapper.findUserColection(opusid,puserid);
        Map<String, Object> map = mapper.findDetail(opusid);
        map.put("praise_p_user_id",userPraise);
        map.put("colection_p_user_id",userColection);
        return map;
    }
/**
* 方法实现说明 删除作品
* @author      ytw
*@param
* @return
* @exception
* @date        2019/7/31 22:16
*/
    @Transactional(rollbackFor=Exception.class)
    @Override
    public Integer deleteOpus(Integer opusid) {
        return writeMapper.deletebyopusid(opusid);
    }
/**
* 方法实现说明  提交作品
* @author      ytw
*@param
* @return
* @exception
* @date        2019/7/31 22:16
*/
    @Transactional(rollbackFor=Exception.class)
    @Override
    public Integer createOpus(Map map) {
        writeMapper.insertopus(map);
        return writeMapper.insertopusource(map);
    }

    /**
    * 方法实现说明  上传图片
    * @author      ytw
    *@param
    * @return
    * @exception
    * @date        2019/7/31 22:17
    */
    @Override
    public String fileUpLoad(MultipartFile file, Integer userId) {
        String fileName = file.getOriginalFilename();
        String contentType = file.getContentType();
        if(!CONTENT_TYPE.contains(contentType)){
            LOGGER.info("文件类型不合法: {}",fileName);
            return null;
        }
        try {
            BufferedImage bufferedImage = ImageIO.read(file.getInputStream());
            if(bufferedImage==null){
                LOGGER.info("文件内容为空: {}",bufferedImage);
                return null;
            }
            file.transferTo(new File("E:\\image\\" +fileName));
            return "E:\\image\\" +fileName;
        } catch (IOException e) {
            e.printStackTrace();
            LOGGER.info("服务器错误");
        }
        return  null;
    }

}
